package com.demo.supplier.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ配置类
 * 
 * @author demo
 */
@Configuration
public class RabbitMqConfig {

    // 交换机名称
    public static final String LOG_TOPIC_EXCHANGE = "log.topic.exchange";
    
    // 队列名称
    public static final String LOGIN_LOG_QUEUE = "login.log.queue";
    public static final String OPERATION_LOG_QUEUE = "operation.log.queue";
    
    // 路由键
    public static final String LOGIN_LOG_ROUTING_KEY = "log.login";
    public static final String OPERATION_LOG_ROUTING_KEY = "log.operation";

    /**
     * 声明主题交换机
     */
    @Bean
    public TopicExchange logTopicExchange() {
        return new TopicExchange(LOG_TOPIC_EXCHANGE);
    }

    /**
     * 声明登录日志队列
     */
    @Bean
    public Queue loginLogQueue() {
        return QueueBuilder.durable(LOGIN_LOG_QUEUE).build();
    }

    /**
     * 声明操作日志队列
     */
    @Bean
    public Queue operationLogQueue() {
        return QueueBuilder.durable(OPERATION_LOG_QUEUE).build();
    }

    /**
     * 绑定登录日志队列到交换机
     */
    @Bean
    public Binding bindingLoginLogQueue() {
        return BindingBuilder.bind(loginLogQueue()).to(logTopicExchange()).with(LOGIN_LOG_ROUTING_KEY);
    }

    /**
     * 绑定操作日志队列到交换机
     */
    @Bean
    public Binding bindingOperationLogQueue() {
        return BindingBuilder.bind(operationLogQueue()).to(logTopicExchange()).with(OPERATION_LOG_ROUTING_KEY);
    }
}